/*
 * Main.java
 * 
 * @version 1.0, April 6, 2011
 * CS 2340 Team 10: Cyberdyne Systems
 */
package edu.gatech.oad.cyberdoc.server;

import java.awt.Component;
import java.awt.EventQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

import edu.gatech.oad.cyberdoc.database.Database;
import edu.gatech.oad.cyberdoc.database.DatabaseException;
import edu.gatech.oad.cyberdoc.database.FlatFileDatabase;
import edu.gatech.oad.cyberdoc.domain.action.LoginHandler;
import edu.gatech.oad.cyberdoc.domain.action.RequestAppointmentHandler;
import edu.gatech.oad.cyberdoc.domain.action.SelectAppointmentHandler;
import edu.gatech.oad.cyberdoc.domain.action.TreatmentRecordHandler;
import edu.gatech.oad.cyberdoc.domain.action.UserHandler;
import edu.gatech.oad.cyberdoc.domain.action.ViewAppointmentsHandler;
import edu.gatech.oad.cyberdoc.domain.action.ViewReportHandler;
import edu.gatech.oad.cyberdoc.domain.action.ViewTreatmentRecordHandler;
import edu.gatech.oad.cyberdoc.domain.action.ViewInvoiceHandler;
import edu.gatech.oad.cyberdoc.domain.action.ViewUsersHandler;
import edu.gatech.oad.cyberdoc.net.ServerConnectionManager;

/**
 * This class serves as the main entry point for the server application. It also
 * stores useful objects such as the database connector.
 * 
 * TODO Serve as the main entry point for the server application
 * 
 * @author John Girata
 * @version 1.0, April 6, 2011
 */
public class Server {

	/**
	 * The logger for this class.
	 */
	private static final Logger LOG;

	/**
	 * The database connector object.
	 */
	private static final Database DATABASE;

	/**
	 * The dummy GUI to let the user easily shutdown the server when running
	 * from Ant.
	 */
	private JFrame frmCyberdocServer;

	static {
		LOG = Logger.getLogger("edu.gatech.oad.cyberdoc.action."
				+ "UserHandler");
	}

	static {
		DATABASE = new FlatFileDatabase();
	}

	/**
	 * Returns the instance of the database connector being used by this
	 * instance of the server.
	 * 
	 * @return The active database connector object
	 */
	public static Database getDatabase() {
		return DATABASE;
	}

	/**
	 * Create the application.
	 */
	public Server() {
		initialize();
	}

	/**
	 * Returns a string representation of this object.
	 * 
	 * @return A string representation of this object
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Main";
	}

	/**
	 * Launch the application.
	 * 
	 * @param args
	 *            Ignored
	 */
	public static void main(String[] args) {
		try {
			LOG.info("Initializing system from database");
			getDatabase().load();
		} catch (DatabaseException e) {
			LOG.log(Level.SEVERE, "Error occurred while loading database", e);
		}

		LOG.info("Starting action handlers");
		new LoginHandler();
		new RequestAppointmentHandler(
				new long[] { 28800000, 30800000, 35800000 }); // $codepro.audit.disable
		new SelectAppointmentHandler();
		new ViewUsersHandler();
		new UserHandler();
		new ViewAppointmentsHandler();
		new ViewTreatmentRecordHandler();
		new ViewReportHandler();
		new ViewInvoiceHandler();
		new TreatmentRecordHandler();

		LOG.info("Starting networking handlers");
		final ServerConnectionManager server = new ServerConnectionManager();
		final Thread serverThread = new Thread(server, "Server Thread");
		serverThread.start();

		LOG.info("Launching GUI");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					final Server window = new Server();
					window.frmCyberdocServer.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCyberdocServer = new JFrame();
		frmCyberdocServer.setTitle("Cyberdoc Server");
		frmCyberdocServer.setBounds(100, 100, 450, 300); // $codepro.audit.disable
		frmCyberdocServer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmCyberdocServer.getContentPane().setLayout(
				new BoxLayout(frmCyberdocServer.getContentPane(),
						BoxLayout.X_AXIS));

		final Component horizontalGlue = Box.createHorizontalGlue();
		frmCyberdocServer.getContentPane().add(horizontalGlue);

		final JLabel lblThisIsA = new JLabel(
				"This is a dummy window to allow you to kill the server.");
		frmCyberdocServer.getContentPane().add(lblThisIsA);

		final Component horizontalGlue_1 = Box.createHorizontalGlue();
		frmCyberdocServer.getContentPane().add(horizontalGlue_1);
	}

}
